﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Crosland.Automotriz.Logic.Linq;
using Crosland.Automotriz.Entidad;

namespace Crosland.Automotriz.Web.CRM
{
    public partial class wFrmClienteCarteraActualizar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false) 
            {
                ListarOrganizacion();
                ListarCanal();
                ListarVendedor();

                if (Request.QueryString["ID_CLIENTE"] != null) 
                {
                    int ID_CLIENTE = int.Parse(Request.QueryString["ID_CLIENTE"].ToString());
                    lblIdCliente.Text = ID_CLIENTE.ToString();
                    clsTD_ClienteBL bl = new clsTD_ClienteBL();
                    TD_CLIENTE cli = new TD_CLIENTE();
                    cli = bl.Listar(ID_CLIENTE);
                    lblNombreCliente.Text = cli.DES_CLIENTE + " - " + cli.NIF;
                    txtCliente.Visible = false;
                    btnConsultar.Visible = false;
                    tablaConsulta.Visible = false;
                    int ID_ORG_VTA = int.Parse(Request.QueryString["o"].ToString());
                    int ID_CANAL_DISTRIB = int.Parse(Request.QueryString["c"].ToString());
                    int ID_VENDEDOR = int.Parse(Request.QueryString["v"].ToString());

                    cboOrganizacion.Value = ID_ORG_VTA.ToString();
                    cboOrganizacion_SelectedIndexChanged(null, null);
                    cboOrganizacion.Enabled = false;
                    cboCanal.Value = ID_CANAL_DISTRIB.ToString();
                    cboCanal.Enabled = false;
                    cboSupervisor.Value = ID_VENDEDOR.ToString();
                    btnRegistrar.Text = "Actualizar";
                }

                Panel img = (Panel)Master.FindControl("asp");
                img.Visible = false;
            }
        }

        void ListarOrganizacion() 
        {
            clsTd_Org_VtaLogic oOrg = new clsTd_Org_VtaLogic();
            cboOrganizacion.DataSource = oOrg.lista();
            cboOrganizacion.TextField = "DES_ORG_VTA";
            cboOrganizacion.ValueField = "ID_ORG_VTA";
            cboOrganizacion.DataBind();
            cboOrganizacion.SelectedIndex = 0;
        }

        void ListarCanal() 
        {
            int ID_ORG_VTA = int.Parse(cboOrganizacion.SelectedItem.Value.ToString());
            clsTd_Canal_DistribLogic oCanal = new clsTd_Canal_DistribLogic();
            cboCanal.DataSource = oCanal.ListarCanalxOrganizacion(ID_ORG_VTA);
            cboCanal.TextField = "DES_CANAL_DISTRIB";
            cboCanal.ValueField = "ID_CANAL_DISTRIB";
            cboCanal.DataBind();
            cboCanal.SelectedIndex = 0;

            if (cboCanal.Items.Count == 0)
            {
                cboCanal.Enabled = false;
                cboSupervisor.Enabled = false;
                txtCliente.Enabled = false;
                btnConsultar.Enabled = false;
                btnRegistrar.Enabled = false;
            }
            else 
            {
                cboCanal.Enabled = true;
                cboSupervisor.Enabled = true;
                txtCliente.Enabled = true;
                btnConsultar.Enabled = true;
                btnRegistrar.Enabled = true;
            }

        }

        void ListarVendedor() 
        {
            clsTD_VendedorBL oVen = new clsTD_VendedorBL();
            cboSupervisor.DataSource = oVen.ListaVendedor();
            cboSupervisor.TextField = "NOMBRE_APELLIDO";
            cboSupervisor.ValueField = "ID_VENDEDOR";
            cboSupervisor.DataBind();
            cboSupervisor.SelectedIndex = 0;
        }

        protected void cboOrganizacion_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListarCanal();
        }

        protected void btnSalir_Click(object sender, EventArgs e)
        {
            string ID_ORG_VTA = "";
            string ID_CANAL_DISTRIB = "";
            string ID_VENDEDOR = "";

            ID_ORG_VTA = Request.QueryString["o"].ToString();
            ID_CANAL_DISTRIB = Request.QueryString["c"].ToString();
            ID_VENDEDOR = Request.QueryString["v"].ToString();

            Response.Redirect("wFrmClienteCartera.aspx?o=" + ID_ORG_VTA + "&c=" + ID_CANAL_DISTRIB + "&v=" + ID_VENDEDOR);
        }

        protected void btnConsultar_Click(object sender, EventArgs e)
        {
            txtCliente2.Text = txtCliente.Text;
            pcBuscar.ShowOnPageLoad = true;
            btnConsultar2_Click(null, null);
        }

        protected void btnConsultar2_Click(object sender, EventArgs e)
        {
            clsTD_ClienteBL oCli = new clsTD_ClienteBL();
            gv.DataSource = oCli.BuscarClientes(txtCliente2.Text.Trim(), int.Parse(cboOpcion.Text));
            gv.DataBind();
        }

        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            int indice = gv.FocusedRowIndex;
            if (indice >= 0)
            {
                txtCliente.Visible = false;
                List<object> keys = gv.GetSelectedFieldValues(new string[] { gv.KeyFieldName });
                int ID_CLIENTE = int.Parse(keys[0].ToString());
                lblIdCliente.Text = ID_CLIENTE.ToString();
                pcBuscar.ShowOnPageLoad = false;
                lblNombreCliente.Text = gv.GetRowValues(indice, "DES_CLIENTE").ToString() + " - " + gv.GetRowValues(indice, "NIF").ToString();
            }
        }

        protected void btnRegistrar_Click(object sender, EventArgs e)
        {
            if (lblIdCliente.Text == "") 
            {
                lblMensaje.Text = "Debe seleccionar primero un cliente";
                popupErrores.ShowOnPageLoad = true;
                return; 
            }
            try
            {
                T_CLIENTE_VENDEDOR ecv = new T_CLIENTE_VENDEDOR();
                ecv.ID_ORG_VTA = int.Parse(cboOrganizacion.SelectedItem.Value.ToString());
                ecv.ID_CANAL_DISTRIB = int.Parse(cboCanal.SelectedItem.Value.ToString());
                ecv.ID_CLIENTE = int.Parse(lblIdCliente.Text);
                ecv.ID_VENDEDOR = int.Parse(cboSupervisor.SelectedItem.Value.ToString());

                clsT_CLIENTE_VENDEDORBL oev = new clsT_CLIENTE_VENDEDORBL();

                IEnumerable<Object>Lista = oev.DatosClienteVendedor(ecv.ID_ORG_VTA, ecv.ID_CANAL_DISTRIB, ecv.ID_CLIENTE);
                int NroFilas=Lista.Count();


                if (Request.QueryString["ID_CLIENTE"] != null)
                {
                    oev.Actualizar(ecv.ID_ORG_VTA, ecv.ID_CANAL_DISTRIB, ecv.ID_CLIENTE, ecv.ID_VENDEDOR);
                }
                else if(NroFilas>0)
                {
                    oev.Actualizar(ecv.ID_ORG_VTA, ecv.ID_CANAL_DISTRIB, ecv.ID_CLIENTE, ecv.ID_VENDEDOR);
                }
                else
                {
                    oev.Insertar(ecv);
                }
                
                btnRegistrar.Enabled = false;
                cboSupervisor.Enabled = false;
                cboCanal.Enabled = false;
                cboOrganizacion.Enabled = false;
                btnConsultar.Enabled = false;

                lblMensaje.Text = "Se grabaron los datos satisfactoriamente.";
                popupErrores.ShowOnPageLoad = true;
            }
            catch (Exception)
            {

            }


        }
    }
}